package oh.yeah.monkey;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Locale;
import java.util.StringTokenizer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

import android.text.TextUtils;
import oh.yeah.monkey.Constant;
import android.os.*;


public class WebHandler implements HttpRequestHandler
{
    private String webRoot;
    public WebHandler(String webRoot) {
        this.webRoot = webRoot;
    }

    @Override
    public void handle(HttpRequest httpRequest, HttpResponse httpResponse,
					   HttpContext httpContext) throws HttpException, IOException
    {

	 
	 String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String target = sdCardPath+URLDecoder.decode(httpRequest.getRequestLine().getUri(),
										  Constant.ENCODING);
										  
										  
        final File file = new File(target);
        HttpEntity entity = new StringEntity("", Constant.ENCODING);
        String contentType = "text/html;charset=" + Constant.ENCODING;
        if (!file.exists()){
            httpResponse.setStatusCode(HttpStatus.SC_NOT_FOUND);
        }else if (file.canRead()){
            httpResponse.setStatusCode(HttpStatus.SC_OK);
            if (file.isDirectory()){
				String msg = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>猴子快传</title></head><body><p>对方向您分享了一些文件:</p><p><a href=\"../\">../</a></p>";
                String[] files = file.list(new FilenameFilter()
					{
						@Override
						public boolean accept(File file, String s)
						{
							return !file.isHidden() && !file.getName().startsWith(".");
						}
					});
				//if (files == null)
                //{
				//	msg += "<p>目录下无文件。</p>";
				//}
                if (files != null)
                {
				
                    for (int i = 0; i < files.length; i++)
                    {
                        File curFile = new File(file, files[i]);
						if (!curFile.isFile())
						{
						 String targeturl=target.replace(sdCardPath, "");
						 String result = targeturl.substring(1);
						 msg += "<a href=\"" + encodeUri(result+ "/" + files[i]) + "\">"
						  + files[i] + "</a><br>";
								
                        }
						}
						
						
						
					for (int i = 0; i < files.length; i++)
                    {
                        File curFile = new File(file, files[i]);
						if (curFile.isFile()){
						 String targeturl=target.replace(sdCardPath, "");
						 String result = targeturl.substring(1);
						 msg += "<a href=\"" + encodeUri(result+ "/" + files[i]) + "\">"
						  + files[i] + "</a>";
						 
						long len = curFile.length();
						msg += " <font size=2><";
						if (len < 1024)
							msg += len + " bytes";
						else if (len < 1024 * 1024)
							msg += len / 1024 + "." + (len % 1024 / 10 % 100) + " KB";
						else
							msg += len / (1024 * 1024) + "." + len % (1024 * 1024)
								/ 10 % 100 + " MB";

						msg += "></font><br>";
					}}
						
       
					}
                
                msg += "<p>Powerd By 猴子快传.</p></body></html>";
                entity = new StringEntity(msg, Constant.ENCODING);
                httpResponse.setHeader("Content-Type", contentType);
            }
			else
			{
                String mime = null;
                int dot = file.getCanonicalPath().lastIndexOf(".");
                if (dot >= 0)
                {
                    mime = (String) Constant.theMimeTypes.get(file.getCanonicalPath()
															  .substring(dot + 1).toLowerCase(Locale.ENGLISH));
                    if (TextUtils.isEmpty(mime))
                        mime = Constant.MIME_DEFAULT_BINARY;
                    httpResponse.setHeader("Content-Type", mime);
                    httpResponse.addHeader("Content-Description", "MonkeyShare");
                    httpResponse.addHeader("Content-Disposition", "attachment;filename="
										   + encodeFilename(file));
                    httpResponse.setHeader("Content-Transfer-Encoding", "binary");
                    entity = new EntityTemplate(new ContentProducer()
						{
							@Override
							public void writeTo(OutputStream outStream) throws IOException
							{
								write(file, outStream);
							}
						});
                }
            }
        }
        else
        {
			httpResponse.setStatusCode(HttpStatus.SC_FORBIDDEN);
        }

        httpResponse.setEntity(entity);
    }

    private String encodeUri(String uri)
    {
        String newUri = "";
        StringTokenizer st = new StringTokenizer(uri, "/ ", true);
        while (st.hasMoreTokens())
        {
            String tok = st.nextToken();
            if (tok.equals("/"))
                newUri += "/";
            else if (tok.equals(" "))
                newUri += "%20";
            else
            {
                newUri += URLEncoder.encode(tok);
            }
        }
        return newUri;
    }

    private void write(File inputFile, OutputStream outStream) throws IOException
    {
        FileInputStream fis = new FileInputStream(inputFile);
        try
        {
            int count;
            byte[] buffer = new byte[Constant.BUFFER_LENGTH];
            while ((count = fis.read(buffer)) != -1)
            {
                outStream.write(buffer, 0, count);
            }
            outStream.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            fis.close();
            outStream.close();
        }
    }

    private String encodeFilename(File file) throws IOException
    {
        String filename = URLEncoder.encode(getFilename(file), Constant.ENCODING);
        return filename.replace("+", "%20");
    }

    private String getFilename(File file)
    {
        return file.isFile() ? file.getName() : file.getName() + ".zip";
    }
}
